﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace Web.Controllers.API
{
    public class MallController : ApiController
    {
        /// <summary>
        /// 销售订单添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult InsertOrder([FromBody]Models.API.Param.SaleOrderModel entity)
        {
            Result<string> result = new Result<string>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.TotalRows = 0;
            result.Data = "";
            try
            {


                string connectionString = "";
                string msg = Common.Common.GetHerdeerConnString(this.Request.Headers, out connectionString);

                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Btype where usercode='" + entity.BTypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到往来单位";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到经手人";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到仓库";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from OrderIndex where BillCode='" + entity.BillCode + "'"))>0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "单据编号重复";
                    return Json(result);
                }
                List<Web.Common.OrderBillModel> l = new List<Common.OrderBillModel>();
                foreach (var item in entity.Details)
                {
                    if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Ptype where usercode='" + item.PTypeUserCode + "' and deleted=0")) == 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "未找到商品编号为[" + item.PTypeUserCode + "]的商品。";
                        return Json(result);
                    }

                    Common.OrderBillModel om = new Common.OrderBillModel();
                    om.chvPTypeID = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Ptype where usercode='" + item.PTypeUserCode + "' and deleted=0").ToString();
                    om.numQty = item.Qty;
                    om.numPrice = item.Price;
                    om.numTotal = item.Qty * item.Price;
                    om.numTax = 0;
                    om.numTaxPrice = item.Price;
                    om.numTaxTotal = om.numTotal;
                    om.intOrderID = -1;
                    om.numDiscountPrice = item.Price;
                    om.numSaleTotal = om.numTotal;
                    om.KTypeID = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0").ToString();
                    om.STypeID = om.STypeID;
                    om.NTaxMoney = 0;
                    om.NSalePrice = item.Price;
                    om.TaxMoney = 0;
                    om.NSaleTotal = item.Price * item.Qty;
                    om.NDiscountPrice = item.Price;
                    om.NTotal = om.numTotal;
                    om.NTaxPrice = item.Price;
                    om.NTaxTotal = om.numTotal;
                    om.UnitId = 1;
                    om.MUnitId = 1;
                    om.MQty = item.Qty;
                    om.MUnitRate = 1;
                    om.MSalePrice = item.Price;
                    om.MDiscountPrice = item.Price;
                    om.MTaxPrice = item.Price;
                    om.CurMSalePrice = item.Price;
                    om.CurMDiscountPrice = item.Price;
                    om.CurMTaxPrice = item.Price;
                    //om.CargoID = 1;//货号修改按仓库查询 2022-06-02 
                    om.CargoID = Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select CargoID from CargoType where KtypeID='" + om.KTypeID + "'"));
                    om.GoodsCostPrice = -1;
                    om.PriceSource = "手工输入";
                    om.OrderId = -1;
                    om.Id = 1;
                    om.ShowOrder = 1;
               
                    l.Add(om);
                }

                Web.Common.OrderIndexParam m = new Common.OrderIndexParam();
                m.BillType = 300;
                m.BillDate = entity.BillDate;
          
                m.BillCode = entity.BillCode;
                m.BtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Btype where usercode='" + entity.BTypeUserCode + "' and deleted=0").ToString();
                m.KtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0").ToString();
                m.EtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0").ToString();
                m.TotalMoney = entity.Details.Sum(p => p.Price * p.Qty);
                m.TotalQty = entity.Details.Sum(p => p.Qty);
                m.ReachDate = entity.BillDate;
                m.Checke = "00001";
                m.DtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select DtypeID from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0").ToString();
                m.STypeID = "00001";
                m.NTotalMoney = m.TotalMoney;
                m.CId = 1;
                m.Rate = 1;
                m.BillProperty = -1;
                m.dealbtypeid = m.BtypeId;
                int BillID = Web.Common.ErpHelper.InsertOrderIndex(m, connectionString);

                if (BillID > 0)
                {
                    try
                    {
                        Common.ErpHelper.InsertOrderBill(0, BillID, 300, l, connectionString);
                    }
                    catch (Exception de)
                    {
                        new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql("delete BillIndex where billNumberID=" + BillID);
                        result.Type = AjaxResultType.Error;
                        result.Content = de.Message;
                        return Json(result);

                    }

                }
                result.Data = "";
                return Json(result);
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                return Json(result);
            }
        }
        /// <summary>
        /// 销售退货单添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult InsertSaleBackBill([FromBody]Models.API.Param.SaleOrderModel entity)
        {
            Result<string> result = new Result<string>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.TotalRows = 0;
            result.Data = "";
            //增加序列号
            try
            {


                string connectionString = "";
                string msg = Common.Common.GetHerdeerConnString(this.Request.Headers, out connectionString);
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Btype where usercode='" + entity.BTypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到往来单位";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到经手人";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0")) == 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "未找到仓库";
                    return Json(result);
                }
                if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from BillIndex where BillCode='" + entity.BillCode + "'")) > 0)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "单据编号重复";
                    return Json(result);
                }
                List<Web.Common.SaleBackModel> l = new List<Common.SaleBackModel>();

                foreach (var item in entity.Details)
                {
                    var db = new FreeADO.DBUtility.DbHelperSQLP(connectionString);
                    string Ktypeid = db.GetSingle("select typeid from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0").ToString();
                    if (!db.Exists("select CargoID from CargoType where deleted=0 and KtypeID='" + Ktypeid + "' and Comment='" + item.CargoCode + "'"))
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "CargoCode[" + item.CargoCode + "]不存在，或此仓库没有这个货位";
                        return Json(result);
                    }


                    int CargoID = Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select CargoID from CargoType where deleted=0 and KtypeID='" + Ktypeid + "' and Comment='" + item.CargoCode + "'"));

                    if (Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select count(*) from Ptype where usercode='" + item.PTypeUserCode + "' and deleted=0")) == 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "未找到商品编号为[" + item.PTypeUserCode + "]的商品。";
                        return Json(result);
                    }

                    Common.SaleBackModel om = new Common.SaleBackModel();
                    
                    om.PtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Ptype where usercode='" + item.PTypeUserCode + "' and deleted=0").ToString();
                    om.Qty = item.Qty;
                    om.SalePrice = item.Price;
                    om.Discount = 1;
                    om.DiscountPrice = item.Price;
                    om.Total = item.Qty * item.Price;
                    om.MakeDate = entity.BillDate;
                    om.Tax = 0;
                    om.TaxPrice = item.Price;
                    om.TaxTotal = om.Total;
                    om.GoodsCostPrice = -1;
                    om.SaleTotal = om.Total;
                    om.KTypeID = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0").ToString();
                    om.STypeID = om.STypeID;
                    om.TaxMoney = 0;
                    om.NTaxMoney = 0;
                    om.NSalePrice = item.Price;
                    om.NSaleTotal = item.Price * item.Qty;
                    om.NDiscountPrice = item.Price;
                    om.NTotal = om.Total;
                    om.NTaxPrice = item.Price;
                    om.NTaxTotal = om.Total;
                    //om.CargoId = 1;//货号修改按仓库查询2022-06-02 
                    om.CargoId = CargoID;
                    om.UnitId = 1;
                    om.MUnitId = 1;
                    om.MQty = item.Qty;
                    om.Serial = "я" + item.Serial.Replace(",", "я") + "я";//2022-05-30 添加序列号字段яAGNMBB1705002545я
                    om.MUnitRate = 1;
                    om.MSalePrice = item.Price;
                    om.MDiscountPrice = item.Price;
                    om.MTaxPrice = item.Price;
                    om.CurMSalePrice = item.Price;
                    om.CurMDiscountPrice = item.Price;
                    om.CurMTaxPrice = item.Price;
                    om.ETypeID= new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0").ToString();
                    om.PriceSource = "手工输入";
                    om.ShowOrder = 1;
                    l.Add(om);
                }

                Web.Common.BillIndexParam m = new Common.BillIndexParam();
                m.BillType = 45;
                m.BillDate = entity.BillDate;
                m.BillCode = entity.BillCode;
                m.BtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Btype where usercode='" + entity.BTypeUserCode + "' and deleted=0").ToString();
                m.KtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from Stock where usercode='" + entity.KTypeUserCode + "' and deleted=0").ToString();
                m.EtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select typeid from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0").ToString();
                m.CheckE = "00001";
                m.TotalMoney = entity.Details.Sum(p => p.Price * p.Qty);
                m.TotalQty = entity.Details.Sum(p => p.Qty);
                m.szPeriod = 1;
                m.Draft = true;
                m.JieJianBillNumberID = "-1";
                m.JieJianID = "-1";
                m.BaoXiuBillNumberID = "-1";
                m.BaoXiuID = "-1";
                m.completedate =Convert.ToDateTime("9999-12-31 23:59:59.997");
                m.billstate = 1;
                m.IfCheck = "f";               
                m.IfAudit = "3";
                m.otherinouttype = -1;
                m.DtypeId = new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select DtypeID from employee where usercode='" + entity.ETypeUserCode + "' and deleted=0").ToString();
                m.STypeID = "00001";
                m.ntotalmoney = m.TotalMoney;
                m.cid = 1;
                m.rate = 1;
                m.billproperty = -1;
                m.DEALBTYPEID = m.BtypeId;
                m.atypeid1 = "0000100006";
                int BillID = Web.Common.ErpHelper.InsertBillIndex(m, connectionString);
                if (BillID > 0)
                {
                    try
                    {
                        foreach (var item in l)
                        {
                            item.BillNumberId = BillID;
                        }
                        Common.ErpHelper.InsertSaleBackBill(l, connectionString);
                    }
                    catch (Exception de)
                    {
                        new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql("delete BillIndex where billNumberID=" + BillID);
                        result.Type = AjaxResultType.Error;
                        result.Content = de.Message;
                        return Json(result);

                    }
                    try
                    {
                        foreach (var item in entity.Details)
                        {
                            string sql = "update GP_CruPTypeSerial set State=1  where Serial='"+item.Serial+"'";
                            new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql(sql);
                        }
                    }
                    catch(Exception e)
                    {
                        new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql("delete BillIndex where billNumberID=" + BillID);
                        new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql("delete saleBackBill where billNumberID=" + BillID);
                        result.Type = AjaxResultType.Error;
                        result.Content = e.Message;
                        return Json(result);
                    }

                }
                result.Data = "";
                return Json(result);
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                return Json(result);
            }
        }
        /// <summary>
        /// 查询往来单位列表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult QueryBtype([FromBody]Models.API.Param.QueryCommon entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.TotalRows = 0;
            result.Data = null;
            try
            {


                string connectionString = "";
                string msg = Common.Common.GetHerdeerConnString(this.Request.Headers, out connectionString);

                result.TotalRows = Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select Count(*) from btype where sonnum=0 and deleted=0 and (UserCode like '%" + entity.Search + "%' or FullName like '%" + entity.Search + "%')"));
                result.Data = new FreeADO.DBUtility.DbHelperSQLP(connectionString).Query(@"
select UserCode,FullName from 
(
select ROW_NUMBER() over(order by bid) as RowID, UserCode,FullName 
from btype where sonnum=0 and deleted=0 and (UserCode like '%" + entity.Search + "%' or FullName like '%" + entity.Search + @"%')
) as a
where a.RowID>(" + entity.PageIndex + "-1)*" + entity.PageSize + @" and a.RowID<=" + entity.PageIndex + "*" + entity.PageSize + @"
").Tables[0];
                return Json(result);
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                return Json(result);
            }
        }
        /// <summary>
        /// 订单取消
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult StopOrder([FromBody]Models.API.Param.SaleOrderModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.TotalRows = 0;
            result.Data = null;
            try
            {
                string connectionString = "";
                string msg = Common.Common.GetHerdeerConnString(this.Request.Headers, out connectionString);
                int BillID = Convert.ToInt32(new FreeADO.DBUtility.DbHelperSQLP(connectionString).GetSingle("select BillNumberID from OrderIndex where BillCode='"+ entity.BillCode + "'"));
                new FreeADO.DBUtility.DbHelperSQLP(connectionString).ExecuteSql(@"exec Gp_DelOrderBill @OrderBillNumberID=" + BillID + ",@OrderBilltype='300',@ordertype=0,@chvExcType='Stop',@stopreason='取消订单',@deldate='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',@deletypeid='00001',@delip='::1'");
                return Json(result);
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                return Json(result);
            }
        }
        /// <summary>
        /// 查询订单列表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult QueryOrderList([FromBody]Models.API.Param.QueryOrderInput entity)
        {
            Result<List<Models.API.Param.QueryOrder>> result = new Result<List<Models.API.Param.QueryOrder>>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            result.TotalRows = 0;
            result.Data = null;
            try
            {
                string sql = "";

                sql += " declare @PageIndex int=1; ";
                sql += "  declare @PageRows int=5000000; ";
                sql += " declare @BtypeCode nvarchar(max) = '"+ entity.BtypeCode+ "'; ";
                sql += " declare @EtypeCode nvarchar(max) = '" + entity.EtypeCode + "'; ";
                sql += " declare @DtypeCode nvarchar(max) = '" + entity.DtypeCode + "'; ";
                sql += " declare @KtypeCode nvarchar(max) = '" + entity.KtypeCode + "'; ";
                sql += " declare @BillCode nvarchar(max) = '" + entity.BillCode + "'; ";
                sql += "  with OBD as ( ";
                sql += " select  OI.billNumberId,OI.Billcode,OI.BillDate, ";
                sql += "  OI.btypeid,isnull(btype.UserCode, '') as BtypeCode,isnull(btype.FullName, '') as BtypeName, ";
                sql += " OI.etypeid,isnull(e.UserCode, '') as EtypeCode,isnull(e.FullName, '') as EtypeName, ";
                sql += "  OI.DtypeId,isnull(d.usercode, '') as DtypeCode,isnull(d.FullName, '') as DtypeName, ";
                sql += "  OI.KtypeId,isnull(Stock.UserCode, '') as KtypeCode,isnull(Stock.FullName, '') as KtypeName ";
                sql += "  ,OB.OrderID,OB.ptypeid,p.UserCode as PtypeCode,p.FullName as PtypeName,OB.qty ,OB.price,OB.total,OB.UnitID,pu.FullName as UnitName,OB.ReachQty ";
                sql += " from OrderIndex as OI inner join OrderBill as OB on OI.billNumberId = OB.billNumberID ";
                sql += "  inner join btype on OI.btypeid = btype.typeId ";
                sql += " left join employee as e on OI.etypeid = e.typeId ";
                sql += "  left join Department as d on OI.DtypeId = d.typeid ";
                sql += "  left join ptype as p on OB.ptypeid = p.typeId ";
                sql += "  left join PType_Units as pu on Ob.UnitID = pu.UnitsId ";
                sql += " left join Stock on OI.ktypeid = Stock.typeId ";
                sql += " where OI.billtype = 300 ";
                sql += " and(btype.UserCode = @BtypeCode or @BtypeCode = '') ";
                sql += " and(e.UserCode = @EtypeCode or @EtypeCode = '') ";
                sql += " and(d.UserCode = @DtypeCode or @DtypeCode = '') ";
                sql += "  and(Stock.UserCode = @KtypeCode or @KtypeCode = '') ";
                sql += "  and(OI.Billcode = @BillCode or @BillCode = '') ";
                sql += " ), ";
                sql += "  OBI as( ";
                sql += " select Row_number() over(order by BillNumberID desc) as RowID, BillNumberID from OBD group by billNumberId ";
                sql += " ) ";
                sql += " select OBD.*from OBD inner join OBI on obd.billNumberId = OBI.billNumberId ";
                sql += " where OBI.RowID > (@PageIndex - 1) * @PageRows and OBI.RowID <= @PageIndex * @PageRows ";
                sql += " order by OBD.billNumberId desc, OBD.OrderID ";


                string connectionString = "";
                string msg = Common.Common.GetHerdeerConnString(this.Request.Headers, out connectionString);
                var t=  new FreeADO.DBUtility.DbHelperSQLP(connectionString).Query(sql).Tables[0];

                List<Models.API.Param.QueryOrderOutput> orderList= Newtonsoft.Json.JsonConvert.DeserializeObject<List<Models.API.Param.QueryOrderOutput>>(Newtonsoft.Json.JsonConvert.SerializeObject(t));


                var s = orderList.GroupBy(a => a.BillNumberId);


                List<Models.API.Param.QueryOrder> orderlist = new List<Models.API.Param.QueryOrder>();

                foreach (var item in s)
                {

                    Models.API.Param.QueryOrderOutput orderinput= orderList.Where(a=>a.BillNumberId == item.Key).FirstOrDefault();

                    List<Models.API.Param.QueryOrderOutput> orderinputlist = orderList.Where(a => a.BillNumberId == item.Key).ToList();

                    List<Models.API.Param.QueryOderDetail> detillist = new List<Models.API.Param.QueryOderDetail>();

                    Models.API.Param.QueryOrder order = new Models.API.Param.QueryOrder();
                    order.BillNumberId = orderinput.BillNumberId;
                    order.Billcode = orderinput.Billcode;
                    order.BillDate = orderinput.BillDate;
                    order.BtypeCode = orderinput.BtypeCode;
                    order.BtypeName= orderinput.BtypeName;
                    order.EtypeCode = orderinput.EtypeCode;
                    order.EtypeName = orderinput.EtypeName;
                    order.DtypeCode = orderinput.DtypeCode;
                    order.DtypeName = orderinput.DtypeName;
                    order.KtypeCode = orderinput.KtypeCode;
                    order.KtypeName = orderinput.KtypeName;


                    foreach (var item2 in orderinputlist)
                    {

                        Models.API.Param.QueryOderDetail oderDetail = new Models.API.Param.QueryOderDetail();
                        oderDetail.PtypeCode = item2.PtypeCode;
                        oderDetail.PtypeName = item2.PtypeName;
                        oderDetail.qty = item2.qty;
                        oderDetail.price = item2.price;
                        oderDetail.total = item2.total;
                        oderDetail.UnitName = item2.UnitName;
                        oderDetail.ReachQty = item2.ReachQty;
                        detillist.Add(oderDetail);
                    }

                    order.orderDetail = detillist;
                    orderlist.Add(order);

                }

                var date = (orderlist.Skip((entity.PageIndex - 1) * entity.PageSize).Take(entity.PageSize)).ToList();

                result.Data = date;
                result.TotalRows = orderlist.Count;

                return Json(result);
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                return Json(result);
            }
        }


    }
}
